package com.gxuwz.medical.web.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.math.NumberUtils;

import com.gxuwz.medical.domain.policy.Policy;

/**
 * 
 * @author 演示
 * 
 */
public class PolicyServlet extends BaseServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String m = req.getParameter("m");// 请求处理动作类型:list:显示列表；get：根据ID读取记录;input:新增数据;add:保存新记录；edit：更新记录
		String url = ERROR_PATH;
		try {
			if ("list".equals(m)) {
				url = "/page/policy/policy_list.jsp";
			} else if ("input".equals(m)) {
				url = "/page/policy/policy_add.jsp";
			} else if ("get".equals(m)) {
				url = "/page/policy/policy_edit.jsp";
			} else if ("add".equals(m)) {
				url = this.add(req, resp);
			} else if ("del".equals(m)) {
				url = this.del(req, resp);
			} else if ("allowed".equals(m)) {
				url = this.setup(req, resp, "1");
			} else if ("unallowed".equals(m)) {
				url = this.setup(req, resp, "0");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		process(req, resp, url);

	}

	private String add(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {

		// 1：接收参数
		String runyear = req.getParameter("runyear");
		String maxlinestr = req.getParameter("maxline");
		String status = req.getParameter("status");
		String remark = req.getParameter("remark");
		double maxline = 0.00;
		if (NumberUtils.isNumber(maxlinestr)) {
			maxline = Double.parseDouble(maxlinestr);
		}
		// 2:构造对象
		Policy model = new Policy(runyear, maxline, status, remark);
		// 3：调用保存的方法
		try {
			model.add();
			return "/page/policy/policy_list.jsp";
		} catch (Exception e) {
			throw e;
		}

	}

	private String del(HttpServletRequest req, HttpServletResponse resp)
			throws Exception {

		String runyear = req.getParameter("id");
		Policy model = new Policy();

		try {
			model.delPolicy(runyear);
			return "/page/policy/policy_list.jsp";
		} catch (Exception e) {
			throw e;
		}
	}

	private String setup(HttpServletRequest req, HttpServletResponse resp,
			String status) throws Exception {

		// 1：接收参数
		String runyear = req.getParameter("id");
		// 2:构造对象
		Policy model = new Policy(runyear);
		model.setup(status);
		return "/page/policy/policy_list.jsp";

	}
}
